package com.hskn.hss.utils;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Data
public class BTable {

    private static final long serialVersionUID = 1L;

    private String time;
    private String name;
    private String carTypeName;
    private Object data;

    public BTable() {
    }

    public BTable(String time) {
        this.time = time;
    }

    public BTable(String time, Object data) {
        this.time = time;
        this.data = data;
    }

    public BTable(String time, Object data, String carTypeName) {
        this.time = time;
        this.data = data;
        this.carTypeName = carTypeName;
    }

    public BTable(String time, String name, String carTypeName, Object data) {
        this.time = time;
        this.name = name;
        this.carTypeName = carTypeName;
        this.data = data;
    }

    public static Map<String, Integer> filterByCarType(List<BTable> bTableList, List<String> nameList) {
        if (CollectionUtils.isEmpty(bTableList)) {
            return new HashMap<>();
        }

        Map<String, Integer> map = new HashMap<>();
        for (BTable bTable : bTableList) {
            String carTypeName = bTable.getCarTypeName();
            if (StringUtils.isEmpty(carTypeName)) {
                continue;
            }
            Integer currentNum = Integer.parseInt(bTable.getData().toString());
            Integer num = map.get(carTypeName);
            if (null == num) {
                map.put(carTypeName, currentNum);
            } else {
                map.put(carTypeName, num + currentNum);
            }
        }
        for (String name : nameList) {
            Integer count = map.get(name);
            if (null == count) {
                map.put(name, 0);
            }
        }
        map.remove("0");
        return map;
    }
}
